package com.xinghai.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.xinghai.common.constant.CacheConstants;
import com.xinghai.common.core.redis.RedisCache;
import com.xinghai.common.exception.ServiceException;
import java.io.IOException;

import com.xinghai.common.utils.DateUtils;
import com.xinghai.common.utils.StringUtils;
import com.xinghai.system.domain.vo.QdStoreVo;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.xinghai.common.constant.GenConstants;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xinghai.common.annotation.Log;
import com.xinghai.common.core.controller.BaseController;
import com.xinghai.common.core.domain.AjaxResult;
import com.xinghai.common.enums.BusinessType;
import com.xinghai.system.domain.QdStore;
import com.xinghai.system.service.IQdStoreService;
import com.xinghai.common.utils.poi.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;
import com.xinghai.common.core.page.TableDataInfo;

/**
 * 店铺Controller
 *
 * @author xinghai
 * @date 2024-12-04
 */
@RestController
@RequestMapping("/system/qdStore")
public class QdStoreController extends BaseController
{
    @Autowired
    private IQdStoreService qdStoreService;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询店铺列表
     */
    @PreAuthorize("@ss.hasPermi('system:qdStore:list')")
    @GetMapping("/list")
    public TableDataInfo list(QdStore qdStore)
    {
        startPage();
        List<QdStore> list = qdStoreService.list(qdStore);
        return getDataTable(list);
    }

    /**
     * 查询店铺列表
     */
    @GetMapping("/getList")
    public AjaxResult getList(QdStore qdStore)
    {
        List<QdStore> list = qdStoreService.list(qdStore);
        return success(list);
    }

    /**
     * 查询店铺列表
     */
    @GetMapping("/refreshData")
    public AjaxResult refreshData(String storeId)
    {

        if (StringUtils.isNotBlank(storeId)){
            QdStore store = qdStoreService.getById(storeId);
            if (store!=null){
                QdStoreVo dto = new QdStoreVo();
                BeanUtils.copyProperties(store, dto);
                dto.setReservationStartDateDay(DateUtils.getDate()+" "+store.getReservationStartDate());
                dto.setReservationEndDateDay(DateUtils.getDate()+" "+store.getReservationEndDate());
                dto.setSaleStartDateDay(DateUtils.getDate()+" "+store.getSaleStartDate());
                dto.setSaleEndDateDay(DateUtils.getDate()+" "+store.getSaleEndDate());
                redisCache.setCacheObject(CacheConstants.STORE_INFO + store.getId(), dto);
                return success();
            }
            return error("店铺不存在");
        }
        List<QdStore> list = qdStoreService.list(new QdStore());
        list.forEach(m -> {
            QdStoreVo dto = new QdStoreVo();
            BeanUtils.copyProperties(m, dto);
            dto.setReservationStartDateDay(DateUtils.getDate()+" "+m.getReservationStartDate());
            dto.setReservationEndDateDay(DateUtils.getDate()+" "+m.getReservationEndDate());
            dto.setSaleStartDateDay(DateUtils.getDate()+" "+m.getSaleStartDate());
            dto.setSaleEndDateDay(DateUtils.getDate()+" "+m.getSaleEndDate());
            redisCache.setCacheObject(CacheConstants.STORE_INFO + m.getId(), dto);
        });
        return success();
    }

    /**
     * 导出店铺列表
     */
    @PreAuthorize("@ss.hasPermi('system:qdStore:export')")
    @Log(title = "店铺", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QdStore qdStore)
    {
        List<QdStore> list = qdStoreService.list(qdStore);
        ExcelUtil<QdStore> util = new ExcelUtil<QdStore>(QdStore.class);
        util.exportExcel(response, list, "店铺数据");
    }

    /**
     * 获取店铺详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:qdStore:query')")
    @GetMapping("/getInfo")
    public AjaxResult getInfo(Long id)
    {
        return success(qdStoreService.getById(id));
    }

    /**
     * 新增店铺
     */
    @PreAuthorize("@ss.hasPermi('system:qdStore:add')")
    @Log(title = "店铺", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    public AjaxResult add(@RequestBody QdStore qdStore)
    {
        if (qdStore.getGroupOneImage()==null||qdStore.getGroupOneName()==null){
            throw new ServiceException("请上传商品");
        }
        return toAjax(qdStoreService.saveInfo(qdStore));
    }

    /**
     * 修改店铺
     */
    @PreAuthorize("@ss.hasPermi('system:qdStore:edit')")
    @Log(title = "店铺", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody QdStore qdStore)
    {
        return toAjax(qdStoreService.updateById(qdStore));
    }

    /**
     * 删除店铺
     */
    @PreAuthorize("@ss.hasPermi('system:qdStore:remove')")
    @Log(title = "店铺", businessType = BusinessType.DELETE)
	@PostMapping("/delete")
    public AjaxResult delete(Long id)
    {
        return toAjax(qdStoreService.delete(id,GenConstants.DELETE_FLAG_TRUE));
    }


    /**
     * 批量删除店铺
     */
    @PreAuthorize("@ss.hasPermi('system:qdStore:remove')")
    @Log(title = "店铺", businessType = BusinessType.DELETE)
    @PostMapping("/deleteBatch")
    public AjaxResult deleteBatch(Long[] ids)
    {
        return toAjax(qdStoreService.deleteBatch(ids,GenConstants.DELETE_FLAG_TRUE));
    }

    /**
     * 导入店铺模板
     */
    @PreAuthorize("@ss.hasPermi('system:qdStore:import')")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<QdStore> util = new ExcelUtil<QdStore>(QdStore.class);
        util.importTemplateExcel(response, "店铺数据模板");
    }


    /**
     * 导入店铺
     */
    @Log(title = "店铺管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:qdStore:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        ExcelUtil<QdStore> util = new ExcelUtil<QdStore>(QdStore.class);
        List<QdStore> qdStoreList = null;
        try {
            qdStoreList = util.importExcel(file.getInputStream());
        } catch (IOException e) {
            throw new ServiceException("导入数据失败");
        }
        return success(qdStoreService.importData(qdStoreList));
    }
}
